# m='''
#      *
#
# *       *
# '''
# print(m)
#
# n='''
# *      *
#
# *      *
# '''
# print(n)

# age=input("请输入你的年龄:")
# height=180
# print(f"你输入的年龄是:{age}")
# print("你输入的年龄是：" + age + "是个小孩")
# print("你的年龄是：%s，你的身高是：%d" % (age,height))
# print("年龄%s，%%" %age)
# print("\\")

# a = 101
# b = 3
# print(a%b)


# set1 = {11,22,33,44,55}
# set2 = {3,65,55,44,52,54,86,32}
# print ( set1 |  set2 )
# print ( set1 &  set2 )
# print ( set1 -  set2 )
# print ( set1 ^  set2 )


# str = "asi"
# print(len(str))

# a=1
# if a != 0:
#     print(a)

# list = [23,43,4,2]
# print(len(list))

# c=1
# list = [{'a':'12','b':'43'},{'a':'4','b':'2'}]
# n = list[0]['a']
# m = '转账'+str(c)
# list[0]['a']= m+n
# print(list)

# html = """
# <a href='http://www.woniuxy.com'>蜗牛学院</a>
# 不需要转义
# """
# print(html)
# print('\\')
# print('//')

# def check_phone(phone):
#     isphone = True
#     if len(phone) != 11:
#         isphone = False
#         print("a")
#     for i in range(0,len(phone)):
#         if ord(phone[i]) < 48 or ord(phone[i]) > 57:
#             isphone = False
#             print("b")
#             break
#     print(isphone)
#     return isphone
# phone = input("电话：")
# a = check_phone(phone)
# print(a)
# if a == False:
#     print("输入的用户信息有误")
# else:
#     print("注册成功！")

# with open(r'D:\test.txt',mode='r+') as file:
#     content = file.read()
#     print(content)

# string = "hellochengdu"
# # print(string[::-1])
# import os
# user_path = os.path.abspath('.')+'\\user.txt'

# 读取用户信息放入列表中
# user_file = open(user_path)
# user_list = user_file.readlines()
# print(user_list)
# user_listimport time
# time_now = time.strftime('%Y-%m-%d %H:%M:%S')
# print(type(time_now))
#
# list = [12,42,'23']
# print(type(list[0]))




# 定义全局变量用于保存登录用户的id
login_id = -1

# 判断一个字符串能否被转换为一个有效的数字
def check_number(string):
    point = 0
    isvalid = True
    for c in string:
        code = ord(c)
        if (code < 48 and code != 46) or code > 57:
            isvalid = False
            break
        elif code == 46:
            point += 1
    if point > 1: isvalid = False
    return isvalid

# 创建数据库连接对象
def create_obj():
    db = pymysql.connect("localhost","root","","woniubook",charset="utf8")
    cur = db.cursor()
    return db,cur

# 对数据库的增、删、改
def db_operation(sql):
    db,cur = create_obj()
    cur.execute(sql)
    db.commit()
    cur.close()
    db.close()

# 对数据库的查询(一条数据）
def query_one(sql):
    db,cur = create_obj()
    cur.execute(sql)
    db.commit()
    content = cur.fetchone()
    cur.close()
    db.close()
    return content

# 对数据库的查询(所有数据）
def query_all(sql):
    db,cur = create_obj()
    cur.execute(sql)
    db.commit()
    content = cur.fetchall()
    cur.close()
    db.close()
    return content

def woniubook():
    # 注册
    def reg():
        username = input("[注册]请输入用户名：")
        pwd = input("[注册]请输入密码：")
        phone = input("[注册]请输入手机号码：")
        addr = input("[注册]请输入住址：")
        result = query_one("select username from customer where username = '%s'" % username)
        if result != None:
            print("用户名已存在")
        else:
            db_operation("insert into customer(username,pwd,phone,addr) values ('%s','%s','%s','%s')" % (username, pwd, phone,addr))
            print("注册成功")
            start_menu()

    # 登录
    def login():
        username = input("[登录]请输入用户名：")
        pwd = input("[登录]请输入密码：")
        result = query_one("select pwd from customer where username = '%s'" %username)
        if result != None:
            if result[0] == pwd:
                print("登录成功")
                # 修改全局变量为当前登录用户id
                global login_id
                login_id = query_one("select customerid from customer where username = '%s'" %username)
                main_menu()
            else:
                print("密码错误")
                start_menu()
        else:
            print("用户不存在")
            login()

    #图书入库
    def book_in():
        # 验证身份，如果登录id为1则是管理员，否则无权入库
        if login_id[0] == 1:
            title = input("[入库]请输入书名：")
            author = input("[入库]请输入作者名：")
            publish = input("[入库]请输入出版社：")
            costprice = input("[入库]请输入进价：")
            saleprice = input("[入库]请输入售价：")
            count1 = input("[入库]请输入数量：")
            # 将图书信息写入图书信息表（bug）
            db_operation( "insert into book(title,author,publish,costprice,saleprice,count1) values ('%s','%s','%s',%f,%f,%d)" % (title,author,publish,float(costprice),float(saleprice),int(count1)))
            print("入库成功")
        else:
            print("对不起，非管理员不能入库")
        main_menu()

    # 浏览图书
    def scan():
        result = query_all("select * from book where count1 > 0")
        for book_info in result:
            print(book_info)
        # 选择图书并加入购物车
        bid = input("请输入要购买的图书id：")
        scount = input("请输入要购买的图书数量：")
        saleprice = query_one("select saleprice from book where bookid = %d "%(int(bid)))
        time_now = time.strftime('%Y-%m-%d %H:%M:%S')
        db_operation("insert into records (customerid,bookid,saleprice,salecount,time) values (%d,%d,%f,%f,'%s')" %(login_id[0],int(bid),saleprice[0],int(scount),time_now))
        print("添加购物车成功，以下为订单信息：")
        cart()

    # 购物车
    def cart():
        result = query_all("select * from records where customerid = %d" %login_id[0])
        for note in result:
            print(note)
        choose = input("请确认是否购买：[请输入'确认'或'返回']")
        if choose == '确认':
        #判断用户的电话，地址，姓名三个关键信息是否完善，如果不完善，则提示用户补充完整。
            while '1' == '1':
                username = query_one("select username from customer where customerid = %d" %login_id[0])
                phone = query_one("select phone from customer where customerid = %d" %login_id[0])
                addr = query_one("select addr from customer where customerid = %d" %login_id[0])
                if username != None and phone != None and addr != None:
                    # 图书的购买状态改为1，代表已支付
                    db_operation("update records set status1 = 1 where customerid = %d" % login_id[0])
                    break
                elif username == None:
                    username = input("[登录]请输入用户名：")
                    db_operation("update customer set username = '%s' where customerid = '%s'" %(username,login_id[0]))
                elif phone == None:
                    phone = input("[登录]请输入用户名：")
                    db_operation("update customer set phone = '%s' where customerid = '%s'" %(phone, login_id[0]))
                elif addr == None:
                    db_operation("update customer set phone = '%s' where customerid = '%s'" %(phone, login_id[0]))
        main_menu()


    # 销售记录查询
    def salenote():
        # 验证身份，如果登录id为1则是管理员，否则无权查询
        if login_id[0] == 1:
            result = query_all("select * from records where status1 = 1")
            for note in result:
                print(note)
        else:
            print("对不起，非管理员不能查询")
        main_menu()

# main_menua=3
# b=2.3
# c=int(a)+float(b)
# print(c)
# list = [2,4,6]
# for i in range(0,len(list)):
    # print(i)

# list = [12,14,16]
# for i in range(0,len(list)):
#     print(i,end="\t")
# print('')
# for item in list:
#     print(item,end="\t")

# a = 5
# b = 'srg'
# list = [a,b]
# list_all = []
# list_all.append(list)
# print(list_all)

# list = ['asd',13]
# a = list[0][1:]
# print(a)

# a = "abcdefghijk"
# print(a[3:8:-1])

name='aaa'
print("%s是"%name)